var express = require('express');
var router = express.Router();
var http = require('http');
var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = "mongodb://localhost:27017/douban";

router.post('/submit', function(req, res, next) {
  var title = req.body.title;
  var content = req.body.content;
  var username = req.session.username;
  // var req=req.url.id;
  // console.log(req);
  if(username){
    // 进行添加操作
    MongoClient.connect(DB_CONN_STR,function(err,db){
      if(err){
        return;
      }else{
        var conn = db.collection('comment');
        // var data = {title:title,content:content};
        var data = [{title:title,content:content}]
        conn.insert(data,function(err,results){
          // res.send('comment add success');

          res.send('<script>alert("评论成功"); window.history.back();</script>')
          // res.redirect('/');
          db.close();
        })
      }
    })
  }else{
    res.send('<script>alert("session过期或者未登录"); location.href ="/login"</script>')
  }
}); 

router.get('/detail', function(req, res, next) {

      var pageNo = req.query.pageNo,
        pageNo = pageNo?pageNo:1,
        pageSize = 5,
        count = 0,
        totalPages = 0;

    var findData = function(db,callback){
        // 连接表
       var conn = db.collection('comment');
    
        
        async.parallel([
            function(callback){
                conn.find({}).toArray(function(err,results){
                if(err){
                    return;
                }else{
                    totalPages = Math.ceil(results.length/pageSize);
                    count = results.length;
                    callback(null,results);
                }
                })
            },
            function(callback){
                conn.find({}).sort({_id:-1}).skip( (pageNo-1)*pageSize).limit(pageSize).toArray(function(err,results){
                if(err){
                    return;
                }else{
                    callback(null,results);
                }
                })
            }
            ],function(err,results){
            callback(results[1]);
            })

        }
        async.parallel([
            function(cb){
            MongoClient.connect(DB_CONN_STR,function(err,db){
                if(err){
                    console.log(err)
                    return;
                }else{
                    
                    findData(db,function(results){
                        cb(null,results);
                    })
                }
                })
            },
             function(cb){
               MongoClient.connect(DB_CONN_STR, function (err, db) {
                    if (err) {
                    return;
                    } else {
                    var conn = db.collection('in_theaters');
                    var data = {id:req.query.id};
                    conn.find(data).toArray(function(err,results){
                        cb(null,results)
                    })
                    }
                })
            }
        ],function(err,results){
            res.render('doubanDetail',{
                mesg:results[1],
                pageNo:pageNo,
                totalPages:totalPages,
                list: results[0],
                count:count
            })
        }) 
       
});


           

module.exports = router;
